package com.deman.framework.rpc.config;

import com.deman.framework.rpc.core.MessageSendExecutor;
import com.deman.framework.rpc.netty.ClientStarter;
import com.deman.framework.rpc.properties.RpcSystemConfigProperties;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.Map;

public class DemanRpcReferrenceFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
    private String interfaceName;
    private String serviceName;

    public String getInterfaceName() {
        return interfaceName;
    }
    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    @Override
    public void destroy() throws Exception {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map <String, ServiceNode> map = ServicesNodeManger.build().getNodeMap();
        if(null==map.get(serviceName)){
            map.put(serviceName,new ServiceNode(serviceName));
        }
    }

    @Override
    public Object getObject() throws Exception {
        return MessageSendExecutor.getInstance().execute(getObjectType(),serviceName);
    }

    @Override
    public Class<?> getObjectType() {
        try {
            return this.getClass().getClassLoader().loadClass(interfaceName);
        } catch (Exception e) {
            System.err.println("spring analyze fail!");
        }
        return null;
    }

}
